package com.example.forum.auto.common;

import io.github.bonigarcia.wdm.WebDriverManager;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.Duration;

/**
 * @author hanzishuai
 * Blog :  https://blog.csdn.net/qrwitu142857 [原文地址,请尊重原创]
 * @date 2025/4/20 22:22
 * @Description
 */
public class Utils {
    public static WebDriver driver = null;

    public WebDriverWait wait;


    /**
     * Created by hanzishuai on 2025/4/20
     *
     * @param
     * @return void
     * @Description 创建 Driver
     */
    private WebDriver createChromeDriver() {
        if (driver == null) {
            WebDriverManager.chromedriver().setup();
            ChromeOptions options = new ChromeOptions();
            options.addArguments("--remote-allow-origins=*");
            driver = new ChromeDriver(options);

            // 隐式等待
            driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(15));
        }

        return driver;
    }

    private WebDriver createEdgeDriver() {
        if (driver == null) {
            WebDriverManager.edgedriver().setup();
            EdgeOptions options = new EdgeOptions();
            options.addArguments("--remote-allow-origins=*");
            driver = new EdgeDriver(options);

            // 隐式等待
            driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(15));
        }
        return driver;
    }

    public Utils(String url) {
        if(driver == null) {
            driver = createChromeDriver();
            driver.get(url);
        }
        wait = new WebDriverWait(driver, Duration.ofSeconds(15));
        driver.manage().window().maximize();
    }

    public Utils(String url, BrowserName browserName) {
        if (browserName.getBrowserName().equals(BrowserName.EDGE.getBrowserName())) {
            driver = createEdgeDriver();
            driver.get(url);
        } else if (browserName.getBrowserName().equals(BrowserName.CHROME.getBrowserName())) {
            driver = createChromeDriver();
            driver.get(url);
        }
        wait = new WebDriverWait(driver, Duration.ofSeconds(15));
        driver.manage().window().maximize();
    }

    public void sleep() {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
    public void sleep(Long time) {
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Created by hanzishuai on 2025/4/20
     *
     * @param str 截图名称的前缀
     * @return
     * @Description 屏幕截图
     */
    public void screenShot(String str) throws IOException {
        // 设置文件名和图片名称格式
        SimpleDateFormat sim1 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sim2 = new SimpleDateFormat("HHmmssSS");
        String dirTime = sim1.format(System.currentTimeMillis());
        String fileTime = sim2.format(System.currentTimeMillis());

        String fileName = "src/test/java/com/example/forum/auto/images/" + dirTime + "/" + str + "_" + fileTime + ".png";

        // 截图
        File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(srcFile, new File(fileName));
    }

}
